﻿@{
    
    Layout = "~/Shared/Layouts/_Layout.cshtml";
    Page.Title = "Промени Лозинка";
    
    if (!WebSecurity.IsAuthenticated)
    {
        Response.Redirect("~/Default");
    }

    var currentPassword = "";
    var newPassword1 = "";
    var newPassword2 = "";
   
    if(IsPost)
    {
        currentPassword = Request["currentPassword"];
        newPassword1 = Request["newPassword1"];
        newPassword2 = Request["newPassword2"];
        
        // Validation
        if (currentPassword.IsEmpty()) {
            ModelState.AddError("currentPassword", "Сегашната лозинка е задолжителна.");
        }
        
        if (newPassword1.IsEmpty()) {
            ModelState.AddError("newPassword1", "Задолжително.");
        }
        
        if (newPassword2.IsEmpty()) {
            ModelState.AddError("newPassword2", "Задолжително.");
        }
        
        if(newPassword1 != newPassword2)
        {
            ModelState.AddError("newPassword1", "Лозинките не се совпаѓаат.");
        }
        
        // Attempt password change
        if(ModelState.IsValid)
        {
            var currentUser = WebSecurity.CurrentUserName;
            
            if(WebSecurity.ChangePassword(currentUser, currentPassword, newPassword1))
            {
                Response.Redirect("~/Default");
            }
            else
            {
                ModelState.AddFormError("Неуспешно променување на лозинка.");
            }
        }
    }
}


<h1>Промени Лозинка</h1>
    @Html.ValidationSummary(true)
    <form action="ChangePassword" method="post" class="accountForm">
        <p>
            @Html.Label("Сегашна Лозинка: ", "currentPassword")<br />
            @Html.Password("currentPassword", currentPassword)
            @Html.ValidationMessage("currentPassword")
        </p>
        <p>
            @Html.Label("Нова Лозинка: ", "newPassword1")<br />
            @Html.Password("newPassword1", newPassword1)
            @Html.ValidationMessage("newPassword1")
        </p>
        <p>
            @Html.Label("Потврдете ја Новата Лозинка: ", "newPassword2")<br />
            @Html.Password("newPassword2", newPassword2)
            @Html.ValidationMessage("newPassword2")
        </p>
        <p>
             <input type="submit" value="Промени Лозинка" />
        </p>
    </form>
</body>

